package com.shopping_back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping_back.common.Result;
import com.shopping_back.pojo.dto.ProductDTO;
import com.shopping_back.pojo.dto.SpuSearchDTO;
import com.shopping_back.pojo.entity.Sku;
import com.shopping_back.pojo.entity.Spu;
import com.shopping_back.service.ISpuService;
import com.shopping_back.serviceImpl.SkuServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品SPU表 前端控制器
 * </p>
 *
 * @author Jack
 * @since 2025-07-12
 */
@RestController
@RequestMapping("/api/spu")
@RequiredArgsConstructor
public class SpuController {

    final private ISpuService spuService;
    private SkuServiceImpl skuService;

    @GetMapping
    public Result getProductsByCategory(
            @RequestParam Long categoryId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size){
        return Result.success(spuService.getProductsByCategory(categoryId, page, size));
    }

    @PostMapping("/search")
    public Result search(@RequestBody SpuSearchDTO dto) {
        if (dto.getKeyword() == null || dto.getKeyword().trim().isEmpty()) {
            return Result.error("需输入关键词");
        }
        return Result.success(spuService.search(dto));
    }

    @GetMapping("/pending")
    public Result pending(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(spuService.listPending(pageNum, pageSize));
    }


    @PostMapping("/{spuId}/approve")
    public Result approve(@PathVariable Long spuId) {
        spuService.toggleProductStatus(spuId, 1); // 上架
        return Result.success();
    }

    @PostMapping("/{spuId}/reject")
    public Result reject(@PathVariable Long spuId) {
        spuService.toggleProductStatus(spuId, 0); // 下架
        return Result.success();
    }

    /**
     * 获取已上架的SPU详情及关联的SKU列表
     */
    @GetMapping("/{spuId}/with-sku")
    public Result getSpuWithSkuList(@PathVariable Long spuId) {
        //System.out.println("接收到的 spuId: " + spuId); // 添加日志
        Spu spu = spuService.getSpuWithSkuList(spuId);
        if (spu == null) {
            return Result.error("商品不存在或已下架");
        }
        return Result.success(spu);
    }

    /**
     * 根据规格查询SKU
     */
    @PostMapping("/{spuId}/find-sku")
    public Result findSkuBySpecs(
            @PathVariable Long spuId,
            @RequestBody Map<String, String> specs) {

        try {
            // 查找匹配规格的SKU
            Sku sku = skuService.findSkuBySpecs(spuId, specs);

            if (sku != null) {
                return Result.success(sku);
            } else {
                return Result.error("未找到匹配规格的SKU");
            }
        } catch (Exception e) {
            return Result.error("查询SKU失败: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 创建分页对象
        Page<Spu> page = new Page<>(pageNum, pageSize);

        // 只查询已上架的商品
        LambdaQueryWrapper<Spu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Spu::getStatus, true);

        // 执行分页查询
        IPage<Spu> spuPage = spuService.page(page, queryWrapper);

        return Result.success(spuPage);
    }
}
